package baidu.ui.containers
{
    import baidu.ui.constants.*;
    import baidu.ui.controls.*;
    import baidu.ui.core.*;
    import baidu.ui.events.*;
    import flash.display.*;
    import flash.events.*;
    import flash.geom.*;

    public class ScrollPane extends BUI
    {
        protected var _vScrollBar:ScrollBar;
        protected var _hScrollBar:ScrollBar;
        protected var disabledOverlay:BSkin;
        protected var background:BSkin;
        protected var contentContainer:Sprite;
        protected var contentScrollRect:Rectangle;
        protected var _contentWidth:Number = 0;
        protected var _contentHeight:Number = 0;
        protected var _availableWidth:Number;
        protected var _availableHeight:Number;
        protected var _hScrollPolicy:String;
        protected var _vScrollPolicy:String;
        protected var vScroll:Boolean;
        protected var hScroll:Boolean;
        protected var _hPageScrollSize:Number = 0;
        protected var _vPageScrollSize:Number = 0;
        protected var defaultLineScrollSize:Number = 4;
        public static var defaultStyles:Object = {skin:"ScrollPane_Skin", padding:0};

        public function ScrollPane()
        {
        }

        override public function get classStyles() : Object
        {
            return mergeStyles(BUI.defaultStyles, defaultStyles);
        }

        override public function get enabled() : Boolean
        {
            return super.enabled;
        }

        override public function set enabled(param1:Boolean) : void
        {
            if (this.enabled == param1)
            {
                return;
            }
            this._vScrollBar.enabled = param1;
            this._hScrollBar.enabled = param1;
            super.enabled = param1;
            return;
        }

        public function get availableWidth() : Number
        {
            this.calculateAvailableSize();
            return this._availableWidth;
        }

        public function get availableHeight() : Number
        {
            this.calculateAvailableSize();
            return this._availableHeight;
        }

        public function get contentHeight() : Number
        {
            return this._contentHeight;
        }

        public function set contentHeight(param1:Number) : void
        {
            this._contentHeight = param1;
            return;
        }

        public function get hScrollPolicy() : String
        {
            return this._hScrollPolicy;
        }

        public function set hScrollPolicy(param1:String) : void
        {
            this._hScrollPolicy = param1;
            invalidate(Invalidation.SIZE);
            return;
        }

        public function get vScrollPolicy() : String
        {
            return this._vScrollPolicy;
        }

        public function set vScrollPolicy(param1:String) : void
        {
            this._vScrollPolicy = param1;
            invalidate(Invalidation.SIZE);
            return;
        }

        public function get hLineScrollSize() : Number
        {
            return this._hScrollBar.lineScrollSize;
        }

        public function set hLineScrollSize(param1:Number) : void
        {
            this._hScrollBar.lineScrollSize = param1;
            return;
        }

        public function get vLineScrollSize() : Number
        {
            return this._vScrollBar.lineScrollSize;
        }

        public function set vLineScrollSize(param1:Number) : void
        {
            this._vScrollBar.lineScrollSize = param1;
            return;
        }

        public function get hScrollPosition() : Number
        {
            return this._hScrollBar.scrollPosition;
        }

        public function set hScrollPosition(param1:Number) : void
        {
            drawNow();
            this._hScrollBar.scrollPosition = param1;
            this.setHScrollPosition(this._hScrollBar.scrollPosition);
            return;
        }

        public function set forceHScrollPosition(param1:Number) : void
        {
            this.setHScrollPosition(param1);
            return;
        }

        public function set forceVScrollPosition(param1:Number) : void
        {
            this.setVScrollPosition(param1);
            return;
        }

        public function get vScrollPosition() : Number
        {
            return this._vScrollBar.scrollPosition;
        }

        public function set vScrollPosition(param1:Number) : void
        {
            drawNow();
            this._vScrollBar.scrollPosition = param1;
            this.setVScrollPosition(this._vScrollBar.scrollPosition);
            return;
        }

        public function get maxHScrollPosition() : Number
        {
            drawNow();
            return Math.max(0, this._contentWidth - this._availableWidth);
        }

        public function get maxVScrollPosition() : Number
        {
            return Math.max(0, this._contentHeight - this._availableHeight);
        }

        public function get hPageScrollSize() : Number
        {
            if (isNaN(this._availableWidth))
            {
            }
            return this._hPageScrollSize == 0 && !isNaN(this._availableWidth) ? (this._availableWidth) : (this._hPageScrollSize);
        }

        public function set hPageScrollSize(param1:Number) : void
        {
            this._hPageScrollSize = param1;
            invalidate(Invalidation.SIZE);
            return;
        }

        public function get vPageScrollSize() : Number
        {
            if (isNaN(this._availableHeight))
            {
            }
            return this._vPageScrollSize == 0 && !isNaN(this._availableHeight) ? (this._availableHeight) : (this._vPageScrollSize);
        }

        public function set vPageScrollSize(param1:Number) : void
        {
            this._vPageScrollSize = param1;
            invalidate(Invalidation.SIZE);
            return;
        }

        public function get hScrollBar() : ScrollBar
        {
            return this._hScrollBar;
        }

        public function get vScrollBar() : ScrollBar
        {
            return this._vScrollBar;
        }

        override protected function initUI() : void
        {
            super.initUI();
            setSize(100, 100);
            this.contentScrollRect = new Rectangle(0, 0, 89, 89);
            this.background = new BSkin();
            addChild(this.background);
            this._vScrollBar = new ScrollBar();
            this._vScrollBar.addEventListener(ScrollEvent.SCROLL, this.handleScroll, false, 0, true);
            this._vScrollBar.visible = false;
            this._vScrollBar.lineScrollSize = this.defaultLineScrollSize;
            addChild(this._vScrollBar);
            this._hScrollBar = new ScrollBar();
            this._hScrollBar.orientation = BUIConstants.HORIZONTAL;
            this._hScrollBar.addEventListener(ScrollEvent.SCROLL, this.handleScroll, false, 0, true);
            this._hScrollBar.visible = false;
            this._hScrollBar.lineScrollSize = this.defaultLineScrollSize;
            addChild(this._hScrollBar);
            if (stage)
            {
                stage.addEventListener(MouseEvent.MOUSE_WHEEL, this.handleWheel, false, 0, true);
            }
            else
            {
                this.addEventListener(Event.ADDED_TO_STAGE, this.addedToStageHandler);
            }
            this.addEventListener(Event.REMOVED_FROM_STAGE, this.removeToStageHandlerner);
            this.contentContainer = new Sprite();
            addChild(this.contentContainer);
            this.contentContainer.scrollRect = this.contentScrollRect;
            this.disabledOverlay = new BSkin();
            addChild(this.disabledOverlay);
            this._hScrollPolicy = ScrollPolicy.AUTO;
            this._vScrollPolicy = ScrollPolicy.AUTO;
            return;
        }

        private function removeToStageHandlerner(event:Event) : void
        {
            stage.removeEventListener(MouseEvent.MOUSE_WHEEL, this.handleWheel);
            return;
        }

        private function addedToStageHandler(event:Event) : void
        {
            stage.addEventListener(MouseEvent.MOUSE_WHEEL, this.handleWheel, false, 0, true);
            return;
        }

        protected function setContentSize(param1:Number, param2:Number) : void
        {
            if (this._contentWidth == param1 && this._contentHeight == param2)
            {
                return;
            }
            this._contentWidth = param1;
            this._contentHeight = param2;
            invalidate(Invalidation.SIZE);
            return;
        }

        protected function handleScroll(event:ScrollEvent) : void
        {
            if (event.target == this._vScrollBar)
            {
                this.setVScrollPosition(event.position);
            }
            else
            {
                this.setHScrollPosition(event.position);
            }
            return;
        }

        protected function handleWheel(event:MouseEvent) : void
        {
            if (!this.enabled || !this._vScrollBar.visible || this._contentHeight <= this._availableHeight)
            {
                return;
            }
            if (isNaN(this._vScrollBar.scrollPosition))
            {
                this._vScrollBar.scrollPosition = 0;
            }
            this._vScrollBar.scrollPosition = this._vScrollBar.scrollPosition - event.delta * this.vLineScrollSize;
            this.setVScrollPosition(this._vScrollBar.scrollPosition);
            event.updateAfterEvent();
            dispatchEvent(new ScrollEvent(BUIConstants.VERTICAL, event.delta, this.vScrollPosition));
            return;
        }

        protected function setHScrollPosition(param1:Number) : void
        {
            var _loc_2:* = this.contentContainer.scrollRect;
            _loc_2.x = param1;
            this.contentContainer.scrollRect = _loc_2;
            return;
        }

        protected function setVScrollPosition(param1:Number) : void
        {
            var _loc_2:* = this.contentContainer.scrollRect;
            _loc_2.y = Math.ceil(param1 / this.vLineScrollSize) * this.vLineScrollSize;
            this.contentContainer.scrollRect = _loc_2;
            dispatchEvent(new Event(Event.CHANGE));
            return;
        }

        override protected function draw() : void
        {
            if (isInvalid(Invalidation.STYLES))
            {
                this.drawStyles();
            }
            if (isInvalid(Invalidation.STATE))
            {
                this.drawDisabledOverlay();
            }
            if (isInvalid(Invalidation.SIZE))
            {
                this.drawLayout();
            }
            this.updateChildren();
            super.draw();
            return;
        }

        protected function drawStyles() : void
        {
            var _loc_1:* = getSkinInstance(getStyleValue("skin"));
           // this.background.setStyle("skin", _loc_1["background"]);
           //this.disabledOverlay.setStyle("skin", _loc_1["disabledOverlay"]);
			this.background.setStyle("skin", ResourceFactory.background);
			this.disabledOverlay.setStyle("skin", ResourceFactory.disabledOverlay);
            return;
        }

        protected function drawLayout() : void
        {
            this.calculateAvailableSize();
            this.calculateContentWidth();
            this.background.width = _width;
            this.background.height = _height;
            var _loc_1:* = Number(getStyleValue("padding"));
            if (this.vScroll)
            {
                this._vScrollBar.visible = true;
                this._vScrollBar.x = _width - ScrollBar.WIDTH - _loc_1;
                this._vScrollBar.y = _loc_1;
                this._vScrollBar.height = this._availableHeight;
            }
            else
            {
                this._vScrollBar.visible = false;
            }
            this._vScrollBar.setScrollProperties(this._availableHeight, 0, this._contentHeight - this._availableHeight, this.vPageScrollSize);
            this.setVScrollPosition(this._vScrollBar.scrollPosition);
            if (this.hScroll)
            {
                this._hScrollBar.visible = true;
                this._hScrollBar.x = _loc_1;
                this._hScrollBar.y = _height - ScrollBar.WIDTH - _loc_1;
                this._hScrollBar.width = this._availableWidth;
            }
            else
            {
                this._hScrollBar.visible = false;
            }
            this._hScrollBar.setScrollProperties(this._availableWidth, 0, this._contentWidth - this._availableWidth, this.hPageScrollSize);
            this.disabledOverlay.setSize(_width, _height);
            this.disabledOverlay.setPosition(0, 0);
            this.contentScrollRect = this.contentContainer.scrollRect;
            this.contentScrollRect.width = this._availableWidth;
            this.contentScrollRect.height = this._availableHeight;
            this.contentContainer.scrollRect = this.contentScrollRect;
            var _loc_2:* = _loc_1;
            this.contentContainer.y = _loc_1;
            this.contentContainer.x = _loc_2;
            return;
        }

        protected function drawDisabledOverlay() : void
        {
            this.disabledOverlay.visible = !this.enabled;
            return;
        }

        protected function calculateAvailableSize() : void
        {
            var _loc_1:* = Number(getStyleValue("padding"));
            var _loc_2:* = ScrollBar.WIDTH;
            var _loc_3:* = _height - _loc_1 * 2;
            this.vScroll = this._vScrollPolicy == ScrollPolicy.ON || this._vScrollPolicy == ScrollPolicy.AUTO && this._contentHeight > _loc_3;
            var _loc_4:* = _width - (this.vScroll ? (_loc_2) : (0)) - _loc_1 * 2;
            var _loc_5:* = this._contentWidth - _loc_4;
            this.hScroll = this._hScrollPolicy == ScrollPolicy.ON || this._hScrollPolicy == ScrollPolicy.AUTO && _loc_5 > 0;
            if (this.hScroll)
            {
                _loc_3 = _loc_3 - _loc_2;
            }
            if (this.hScroll && !this.vScroll && this._vScrollPolicy == ScrollPolicy.AUTO && this._contentHeight > _loc_3)
            {
                this.vScroll = true;
                _loc_4 = _loc_4 - _loc_2;
            }
            this._availableHeight = _loc_3;
            this._availableWidth = _loc_4;
            return;
        }

        protected function calculateContentWidth() : void
        {
            return;
        }

        protected function updateChildren() : void
        {
            var _loc_1:* = this.enabled;
            this._hScrollBar.enabled = this.enabled;
            this._vScrollBar.enabled = _loc_1;
            this._vScrollBar.drawNow();
            this._hScrollBar.drawNow();
            this.background.drawNow();
            this.disabledOverlay.drawNow();
            return;
        }

        public function update() : void
        {
            var _loc_1:* = this.contentContainer.getChildAt(0);
            if (_loc_1 is BUI)
            {
                (_loc_1 as BUI).drawNow();
            }
            this.setContentSize(_loc_1.width, _loc_1.height);
            return;
        }

        public function get content() : DisplayObject
        {
            if (this.contentContainer.numChildren > 0)
            {
                return this.contentContainer.getChildAt(0);
            }
            return null;
        }

        public function set content(param1:DisplayObject) : void
        {
            if (this.contentContainer.numChildren > 0)
            {
                this.contentContainer.removeChildAt(0);
            }
            this.contentContainer.addChild(param1);
            this.update();
            return;
        }

        public function get contentWidth() : Number
        {
            return this._contentWidth;
        }

        public function set contentWidth(param1:Number) : void
        {
            this._contentWidth = param1;
            return;
        }

    }
}
